package com.example.movie.ui

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import com.example.movie.R
import kotlinx.android.synthetic.main.activity_welcome.*
import java.util.*
import java.util.concurrent.TimeUnit

class WelcomeActivity : AppCompatActivity() {

    private var defaultWaitTime = 5

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        val handler = Handler(Looper.myLooper()!!)

        //播放app视频
        video.setVideoURI(Uri.parse("android.resource://$packageName/" + R.raw.index))
        video.start()

        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                skip.text = "跳过${defaultWaitTime--}秒"
            }

        }, 0, TimeUnit.SECONDS.toMillis(1))

        val task = Runnable {
            startActivity(Intent(this, MainActivity::class.java))
            video.stopPlayback()
            timer.cancel()
            finish()
        }
        handler.postDelayed(task, TimeUnit.SECONDS.toMillis(5))

        skip.setOnClickListener {
            handler.removeCallbacks(task)
            startActivity(Intent(this, MainActivity::class.java))
            timer.cancel()
            video.stopPlayback()
            finish()
        }

    }


}